#import "Page2.h"
#import "MainView.h"
#import "HTTPProcessor.h"
#import "UIPreferencesTableButton.h"
#import <UIKit/UIPreferencesControlTableCell.h>
#import <UIKit/UIPreferencesDeleteTableCell.h> 

@implementation Page2
- (void) createControls {
	int i;
	for(i = 0; i < 3; i++) {
	dolog("creating buttons %s %s", [[self titleForNumber:i] UTF8String], [[self cellForNumber:i] UTF8String]);
		buttons[i] = [[UIPreferencesTableCell alloc] init];
		[buttons[i] setTitle:[self titleForNumber:i + 1]];
		[buttons[i] setAlignment:2];	
		[buttons[i] setShowSelection:NO];
	}
	buttons[3] = [[UIPreferencesTableButton alloc] init];
	[buttons[3] setTitle:@"Refresh"];
	[buttons[3] setAlignment:2];	
	[buttons[3] setDelegate:self];

	//[processor request:@"get-B1?get-B2?get-B3" delegate:self];
}
 
- (void) buttonPressed:(NSString *)cellData {
	[processor request:@"get-B1?get-B2?get-B3" delegate:self];
}

- (NSString *) cellLetter {
	return @"B";
}

- (NSString *) title {
	return @"Page2";
}

 - (int)preferencesTable:(UIPreferencesTable *)aTable numberOfRowsInGroup:(int)group {
    switch (group) {
        case(0):
            return 4;
            break;
    }
}

- (void) reply:(NSString *)reply {
	dolog("reply received:%s",[reply UTF8String]);
	NSArray *values = [reply componentsSeparatedByString:@"|"];
	int j;
	for(j = 0; j < [values count]; j++) {
		dolog("command: %s",[[values objectAtIndex:j] UTF8String]);
		NSArray *split = [[values objectAtIndex:j] componentsSeparatedByString:@"-"];
		if ([[split objectAtIndex:0] compare:@"OK"] == NSOrderedSame) {
			int i;
			for(i = 0; i < 3; i++) {
				if ([[[buttons[i] title] substringWithRange:NSMakeRange(5, 2)] compare:[split objectAtIndex:1]] == NSOrderedSame) {
					NSString *value = [split objectAtIndex:2];
					[buttons[i] setTitle:[[self titleForNumber:i + 1] stringByAppendingFormat:@" %@",value]];
				}
			}
		}
	}
}
@end
